Computer Science 161.01

October 16th 2019

 

JavaScript Assignment Statements

Use the Scratchpad throughout

 

 

JavaScript Assignment Statements

 

Recall that an expression in JavaScript is just a representation of some value.

For example, 35 is an expression as is 23 % 2.

The first represents, well, the value 35 and the second represents the value 1.

Similarly, if we know x is 40 and y is 15 then  x-2*y is an expression representing 10.

 

Now, an expression in a high-level language is really just one part of a statement, in the same way that a phrase such as “a good grade” is really just

one part of a complete sentence “Tom received a good grade”. 

 

In other words 23 % 2 is not by itself a statement but result = 23 % 2 is.

Such a statement is called an assignment statement because its effect is to assign a value to some location.

Here, the value 1 will be assigned to the location named result.

(Assigning is the same as STOREing on the VSC-32).

 

Assignment Statement Syntax and Semantics

 

By syntax, we mean a rule of grammar.  Unfortunately, when we program, we must conform to strict grammar rules.

If we don’t, we can’t expect JavaScript to understand what we want to do.

 

For an assignment statement, the syntax requires that we have:

            Named-location = expression ;

 

The equal sign is what’s known as the assignment operator.

 

The rule requires that there be a named location (nothing more, nothing less), followed by the assignment operator,

followed by an expression.  That just covers the grammar.  The actions that are caused by the assignment statement

are: 

   1) The expression is evaluated according to the rules you already know.  

   2) The value of the expression is then assigned to (or stored in) the named location on the left hand side of the operator.

 

When we spell out the actions caused by a statement, we describe the meaning (or semantics) of the statement.

Thus, the two steps given above describe the assignment statement semantics.

 

Exercises

  1. Which of the following are legal assignment statements?

x = 23;

y = 10;

y = y + 5;

x = y – 3;

x + 3 = y;

                        x - 1 

   

  1. What actions are caused by the first 4 statements of problem 1 if they occur in sequence?

 

 

Solutions

        x = 23;    is legal… named-location = expression ;  assigns 23 to x

    y = 10;    is legal… named-location = expression;   assigns 10 to       

    y = y + 5; is legal… named-location = expression;   assigns 15 to

    x = y – 3; is legal… named-location = expression;   assigns 12 to x

    x + 3 = y; is NOT LEGAL since x + 3 is not the name of some location.

x – 1;          is NOT a LEGAL assignment statement since x – 1 is just an expression.

 

 

Some Special Shorthand for Certain Assignment Statements

 

Certain types of assignment statements occur so often that a shorthand is often used.

One such is a statement such as x = x + 1.

When we read it, we see that it takes the current value of x, adds 1 to it, and assigns the resulting value to x.

Well, duh, why not just say add 1 to x?!

We can! 

We can simply write x++ and mean precisely x = x + 1.

 

Another shorthand is the use of  x+=20 instead of x = x + 20.

So instead of the long-winded “take the current value of x, add 20 to it, and assign the resulting value to x”,

we will read this as add 20 to x, or, if you prefer increase x by 20.

 

Practice these new ideas by pasting the following sequence of statements into the scratchpad.

You’ll see several assignment statements interspersed with three alert statements.

Each alert statement writes a string to the screen in the form of a popup box as you will see.

More about alert when we discuss JavaScript input and output in some detail.

 

 

//PREDICT THE OUTPUT OF THE FOLLOWING

 

x = 23;

y = 10;

y++;

x+=5;

alert("x = " + x + " y = " + y);

y+=x;

x = y % 5;

alert("x = " + x + " y = " + y);

x++;

y = x + 1;

alert("x = " + x + " y = " + y);